package com.csjbot.snowbot_rogue.camera.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.hardware.Camera.CameraInfo;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.csjbot.snowbot_rogue.camera.CameraInterface;
import com.csjbot.snowbot_rogue.camera.util.Util;

/**
 * Copyright (c) 2017, SuZhou CsjBot. All Rights Reserved.
 * www.csjbot.com
 * <p>
 * Created by 浦耀宗 at 2017/1/9-21:58.
 * Email: puyz@csjbot.com
 */
public class FaceView extends ImageView {
    private Context mContext;
    private Paint mLinePaint;
    private FaceDetector.Face[] mFaces;
    private Matrix mMatrix = new Matrix();

    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        mContext = context;
    }


    public void setFaces(FaceDetector.Face[] faces) {
        this.mFaces = faces;
        invalidate();
    }

    public void clearFaces() {
        mFaces = null;
        invalidate();
    }

    private float myEyesDistance;
    private PointF myMidPoint = new PointF();


    @Override
    protected void onDraw(Canvas canvas) {
        if (mFaces == null || mFaces.length < 1) {
            super.onDraw(canvas);
            return;
        }
        boolean isMirror = false;
        int Id = CameraInterface.getInstance().getCameraId();
        if (Id == CameraInfo.CAMERA_FACING_BACK) {
            isMirror = false; //后置Camera无需mirror
        } else if (Id == CameraInfo.CAMERA_FACING_FRONT) {
            isMirror = true;  //前置Camera需要mirror
        }

        Util.prepareMatrix(mMatrix, isMirror, 90, getWidth(), getHeight());
        canvas.save();
        mMatrix.postRotate(0); //Matrix.postRotate默认是顺时针
        canvas.rotate(-0);   //Canvas.rotate()默认是逆时针

        int previewW = CameraInterface.getInstance().getCameraParams().getPreviewSize().width;
        int previewH = CameraInterface.getInstance().getCameraParams().getPreviewSize().height;
        int screenW = getMeasuredWidth();
        int screenH = getMeasuredHeight();

        for (FaceDetector.Face mFace : mFaces) {
            if (mFace == null) {
                continue;
            }

            myEyesDistance = mFace.eyesDistance();
            mFace.getMidPoint(myMidPoint);
            myMidPoint.x = screenW - myMidPoint.x / previewW * screenW;
            myMidPoint.y = myMidPoint.y / previewH * screenH;
//            Csjlogger.debug("myMidPoint", myMidPoint.toString());
//            Csjlogger.debug("myEyesDistance ", String.valueOf(myEyesDistance));

            canvas.drawRect((int) (myMidPoint.x - myEyesDistance * 2),
                    (int) (myMidPoint.y - myEyesDistance * 2),
                    (int) (myMidPoint.x + myEyesDistance * 2),
                    (int) (myMidPoint.y + myEyesDistance * 2), mLinePaint);
        }

        canvas.restore();
        super.onDraw(canvas);
    }

    private void initPaint() {
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//		int color = Color.rgb(0, 150, 255);
        int color = Color.rgb(98, 212, 68);
//		mLinePaint.setColor(Color.RED);
        mLinePaint.setColor(color);
        mLinePaint.setStyle(Style.STROKE);
        mLinePaint.setStrokeWidth(2f);
        mLinePaint.setAlpha(180);
    }
}
